﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;

namespace Actuals.Data
{
    public class DalEmployeeLeave : Dal<IEmployeeLeave, EmployeeLeave>, IDalEmployeeLeave
    {
        
        
          private readonly IDalManager _manager;
        private static readonly IDalEmployeeLeave _instance = new DalEmployeeLeave();
        private const string SP_GET_EMPLOYEE_LEAVE = "usp_GetEmployeeLeave";
        private const string SP_ADD_EMPLOYEE_LEAVE = "usp_AddEmployeeLeave";
        private const string SP_GETEMPLOYEELEAVE_WITH_EMPID = "usp_GetEmployeeLeaveWithEmpId";
        private const string SP_DELETE_EMPLOYEELEAVE = "usp_Delete_EmployeeLeave";
        private const string SP_GET_EMPLOYEELEAVES_FOR_TEAM = "usp_Get_EmployeeLeaves_For_Team";
        private const string SP_GET_EMPLOYEELEAVES_FOR_TEAM_FROMDATE_TODATE = "usp_Get_EmployeeLeaves_For_Team_FromDate_ToDate";



        public static IDalEmployeeLeave Instance
        {
            get { return _instance; }
        }

        public DalEmployeeLeave()
        {
            _manager = new DalManager();
        }

        public DalEmployeeLeave(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IEmployeeLeave obj, DataRow row)
        {
            obj.EmpId = _manager.GetValue<int>(row, "EmpId");
            
            obj.LeaveType = _manager.GetValue<string>(row, "LeaveType");
            obj.LeaveDate = _manager.GetValue<DateTime>(row, "Leave");
            obj.AM = _manager.GetValue<bool>(row, "AM");
            obj.PM = _manager.GetValue<bool>(row, "PM");
            
        }
     

        #region IDalEmployeeLeave Members

        public IList<IEmployeeLeave> GetEmployeeLeaves(DateTime fromDate, DateTime toDate)
        {  
            _manager.StoredProcedureName = SP_GET_EMPLOYEE_LEAVE;
            _manager.AddInParameter("@FromDate", DbType.DateTime, fromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);

            List<IEmployeeLeave> employeeLeaves = new List<IEmployeeLeave>();
            foreach (IEmployeeLeave employeeLeave in ConvertAll(_manager.ExecuteDataSet()))
                employeeLeaves.Add(employeeLeave);

            return employeeLeaves;
        }

        public IList<IEmployeeLeave> GetEmployeeLeaves(int empId)
        {

            _manager.StoredProcedureName = SP_GETEMPLOYEELEAVE_WITH_EMPID;
            _manager.AddInParameter("@EmpId", DbType.Int32, empId);
            IList<IEmployeeLeave> employeeLeaves = new List<IEmployeeLeave>();
           
            foreach (IEmployeeLeave employeeLeave in ConvertAll(_manager.ExecuteDataSet()))
                employeeLeaves.Add(employeeLeave);

            return employeeLeaves;
        }

        #endregion

        public void AddEmployeeLeave(IEmployeeLeave employeeLeave)
        {
            _manager.StoredProcedureName = SP_ADD_EMPLOYEE_LEAVE;
            _manager.AddInParameter("@EmpId",DbType.Int32, employeeLeave.EmpId);
            _manager.AddInParameter("@LeaveDate",DbType.DateTime, employeeLeave.LeaveDate);
            _manager.AddInParameter("@LeaveType", DbType.String, employeeLeave.LeaveType);
            _manager.AddInParameter("@AM", DbType.Boolean, employeeLeave.AM);
            _manager.AddInParameter("@PM", DbType.Boolean, employeeLeave.PM);
            _manager.ExecuteNonQuery();
            
        }
        public bool DeleteEmployeeLeave(int empId)
        {
            _manager.StoredProcedureName = SP_DELETE_EMPLOYEELEAVE;
            _manager.AddInParameter("@EmpId", DbType.Int32, empId);
            _manager.ExecuteNonQuery();
            return true;

        }

       public IList<IEmployeeLeave> GetEmployeeLeavesForTeam(int teamId)
        {
            _manager.StoredProcedureName = SP_GET_EMPLOYEELEAVES_FOR_TEAM;
            _manager.AddInParameter("@TeamId", DbType.Int32, teamId);
            IList<IEmployeeLeave> employeeLeaves = new List<IEmployeeLeave>();

            foreach (IEmployeeLeave employeeLeave in ConvertAll(_manager.ExecuteDataSet()))
                employeeLeaves.Add(employeeLeave);

            return employeeLeaves;

            
        }

       public IList<IEmployeeLeave> GetEmployeeLeavesForTeam(int teamId, DateTime fromDate, DateTime toDate)
       {
           _manager.StoredProcedureName = SP_GET_EMPLOYEELEAVES_FOR_TEAM_FROMDATE_TODATE;
           _manager.AddInParameter("@TeamId", DbType.Int32, teamId);
           _manager.AddInParameter("@FromDate", DbType.DateTime, fromDate);
           _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);

           IList<IEmployeeLeave> employeeLeaves = new List<IEmployeeLeave>();

           foreach (IEmployeeLeave employeeLeave in ConvertAll(_manager.ExecuteDataSet()))
               employeeLeaves.Add(employeeLeave);

           return employeeLeaves;
       }


    }
}
